package org.linlinjava.litemall.wx.web;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.linlinjava.litemall.core.notify.NotifyService;
import org.linlinjava.litemall.core.notify.NotifyType;
import org.linlinjava.litemall.core.util.CharUtil;
import org.linlinjava.litemall.core.util.JacksonUtil;
import org.linlinjava.litemall.core.util.RegexUtil;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.util.bcrypt.BCryptPasswordEncoder;
import org.linlinjava.litemall.db.domain.LitemallUser;
import org.linlinjava.litemall.db.domain.YeJI;
import org.linlinjava.litemall.db.service.CouponAssignService;
import org.linlinjava.litemall.db.service.LitemallUserService;
import org.linlinjava.litemall.wx.annotation.LoginUser;
import org.linlinjava.litemall.wx.dto.UserInfo;
import org.linlinjava.litemall.wx.dto.UserToken;
import org.linlinjava.litemall.wx.dto.WxLoginInfo;
import org.linlinjava.litemall.wx.service.CaptchaCodeManager;
import org.linlinjava.litemall.wx.service.UserTokenManager;
import org.linlinjava.litemall.core.util.IpUtil;
import org.linlinjava.litemall.wx.util.QrCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.*;

import static org.linlinjava.litemall.wx.util.WxResponseCode.*;

/**
 * 鉴权服务
 */
@RestController
@RequestMapping("/wx/auth")
@Validated
public class WxAuthController {
    private final Log logger = LogFactory.getLog(WxAuthController.class);

    @Autowired
    private LitemallUserService userService;

    @Autowired
    private WxMaService wxService;

    @Autowired
    private NotifyService notifyService;

    @Autowired
    private CouponAssignService couponAssignService;

    /**
     * 账号登录
     *
     * @param body    请求内容，{ mobile: xxx, password: xxx }
     * @param request 请求对象
     * @return 登录结果
     */
    @PostMapping("login")
    public Object login(@RequestBody String body, HttpServletRequest request) {
        String mobile = JacksonUtil.parseString(body, "mobile");
        String password = JacksonUtil.parseString(body, "password");
        if (mobile == null || password == null) {
            return ResponseUtil.badArgument();
        }

        BCryptPasswordEncoder encoder1 = new BCryptPasswordEncoder();
        String encodedPassword = encoder1.encode(password);

        System.out.println("__________________________"+encodedPassword+"________________________________");



        //本来用户名登录
       /* List<LitemallUser> userList = userService.queryByUsername(mobile);*/
        //修改之后用手机号登录
        List<LitemallUser> userList = userService.queryByMobile(mobile);
        LitemallUser user = null;
        if (userList.size() > 1) {
            return ResponseUtil.serious();
        } else if (userList.size() == 0) {
            return ResponseUtil.fail(AUTH_INVALID_ACCOUNT, "账号不存在");
        } else {
            user = userList.get(0);
        }

        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        if (!encoder.matches(password, user.getPassword())) {
            return ResponseUtil.fail(AUTH_INVALID_ACCOUNT, "账号密码不对");
        }

        // 更新登录情况
        user.setLastLoginTime(LocalDateTime.now());
        user.setLastLoginIp(IpUtil.getIpAddr(request));
        if (userService.updateById(user) == 0) {
            return ResponseUtil.updatedDataFailed();
        }

        // userInfo
        UserInfo userInfo = new UserInfo();
        userInfo.setNickName(user.getNickname());
        userInfo.setAvatarUrl(user.getAvatar());

        // token
        String token = UserTokenManager.generateToken(user.getId());

        Map<Object, Object> result = new HashMap<Object, Object>();
        result.put("token", token);
        result.put("userInfo", userInfo);
        result.put("userid", user.getId());
        return ResponseUtil.ok(result);
    }

    /**
     * 微信登录
     *
     * @param wxLoginInfo 请求内容，{ code: xxx, userInfo: xxx }
     * @param request     请求对象
     * @return 登录结果
     */
    @PostMapping("login_by_weixin")
    public Object loginByWeixin(@RequestBody WxLoginInfo wxLoginInfo, HttpServletRequest request) {
        String code = wxLoginInfo.getCode();
        UserInfo userInfo = wxLoginInfo.getUserInfo();
        logger.info("code:" + code);
        logger.info("userInfo:" + userInfo);
        logger.info("id:" + wxLoginInfo.getId());
        if (code == null || userInfo == null) {
            return ResponseUtil.badArgument();
        }

        String sessionKey = null;
        String openId = null;
        try {
            WxMaJscode2SessionResult result = this.wxService.getUserService().getSessionInfo(code);
            sessionKey = result.getSessionKey();
            openId = result.getOpenid();
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (sessionKey == null || openId == null) {
            return ResponseUtil.fail();
        }

        LitemallUser user = userService.queryByOid(openId);
        if (user == null) {
            user = new LitemallUser();
            user.setUsername(openId);
            user.setPassword(openId);
            user.setWeixinOpenid(openId);
            user.setAvatar(userInfo.getAvatarUrl());
            user.setNickname(userInfo.getNickName());
            user.setGender(userInfo.getGender());
            user.setUserLevel((byte) 0);
            user.setStatus((byte) 0);
            user.setLastLoginTime(LocalDateTime.now());
            user.setLastLoginIp(IpUtil.getIpAddr(request));
            user.setSessionKey(sessionKey);

            // 注册用户的时候维护用户链字段
            if (wxLoginInfo.getId() != null){
                LitemallUser directInviteeUser = userService.findById(wxLoginInfo.getId());
                if (directInviteeUser != null){
                    if (directInviteeUser.getUserlian() != null && !Objects.equals(directInviteeUser.getUserlian(), "0")){
                        user.setUserlian(directInviteeUser.getUserlian()+"," + wxLoginInfo.getId());
                    }else {
                        user.setUserlian(wxLoginInfo.getId() + "");
                    }

                }
                // 直推和间推
                String userlian = user.getUserlian();
                if (userlian != null && !userlian.equals("")){
                    String[] arry = userlian.split(",");
                    user.setDirectInvitee(wxLoginInfo.getId());
                    if (arry.length > 1){ // 间推
                        String indirectInvitee = arry[arry.length-2];
                        if (indirectInvitee !=null && !indirectInvitee.equals("")){
                            int indirectInvitee1 = Integer.parseInt(indirectInvitee);
                            user.setIndirectInvitee(indirectInvitee1);
                        }
                    }
                }
            }

            userService.add(user);

            // 新用户发送注册优惠券
            couponAssignService.assignForRegister(user.getId());



        } else {
            user.setLastLoginTime(LocalDateTime.now());
            user.setLastLoginIp(IpUtil.getIpAddr(request));
            user.setSessionKey(sessionKey);
            if (userService.updateById(user) == 0) {
                return ResponseUtil.updatedDataFailed();
            }
        }

        // token
        String token = UserTokenManager.generateToken(user.getId());

        Map<Object, Object> result = new HashMap<Object, Object>();
        result.put("token", token);
        result.put("userInfo", userInfo);
        result.put("userid",user.getId());
        return ResponseUtil.ok(result);
    }


    /**
     * 请求注册验证码
     *
     * TODO
     * 这里需要一定机制防止短信验证码被滥用
     *
     * @param body 手机号码 { mobile }
     * @return
     */
    @PostMapping("regCaptcha")
    public Object registerCaptcha(@RequestBody String body) {

        String phoneNumber = JacksonUtil.parseString(body, "mobile");

        if (StringUtils.isEmpty(phoneNumber)) {
            return ResponseUtil.badArgument();
        }
        if (!RegexUtil.isMobileSimple(phoneNumber)) {
            return ResponseUtil.badArgumentValue();
        }

        if (!notifyService.isSmsEnable()) {
            return ResponseUtil.fail(AUTH_CAPTCHA_UNSUPPORT, "小程序后台验证码服务不支持");
        }

        String code = CharUtil.getRandomNum(6);

        System.out.println("生成的验证码："+code);

        boolean successful = CaptchaCodeManager.addToCache(phoneNumber, code);
        System.out.println(CaptchaCodeManager.getCachedCaptcha(phoneNumber));
        if (!successful) {
            return ResponseUtil.fail(AUTH_CAPTCHA_FREQUENCY, "验证码未超时1分钟，不能发送");
        }
        notifyService.notifySmsTemplate(phoneNumber, NotifyType.JIHUILAI, new String[]{code});

        return ResponseUtil.ok();
    }

    /**
     * 账号注册
     *
     * @param body    请求内容
     *                {
     *                username: xxx,
     *                password: xxx,
     *                mobile: xxx
     *                code: xxx
     *                }
     *                其中code是手机验证码，目前还不支持手机短信验证码
     * @param request 请求对象
     * @return 登录结果
     * 成功则
     * {
     * errno: 0,
     * errmsg: '成功',
     * data:
     * {
     * token: xxx,
     * tokenExpire: xxx,
     * userInfo: xxx
     * }
     * }
     * 失败则 { errno: XXX, errmsg: XXX }
     */
    @PostMapping("register")
    public Object register(@RequestBody String body, HttpServletRequest request) {
        String username = JacksonUtil.parseString(body, "username");
        String password = JacksonUtil.parseString(body, "password");
        String mobile = JacksonUtil.parseString(body, "mobile");
        String code = JacksonUtil.parseString(body, "code");
        String recommender = JacksonUtil.parseString(body, "recommender");
        Integer userId = JacksonUtil.parseInteger(body, "id");

        logger.info("mobile:" + mobile);
        logger.info("recommender:" + recommender);
        logger.info("userId:" + userId);
        logger.info("code:" + code);


        //Integer fromId = Integer.parseInt(id);

        // 如果是小程序注册，则必须非空
        // 其他情况，可以为空
        String wxCode = JacksonUtil.parseString(body, "wxCode");

        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password) || StringUtils.isEmpty(mobile)
                || StringUtils.isEmpty(code)) {
            return ResponseUtil.badArgument();
        }

        List<LitemallUser> userList = userService.queryByUsername(username);
        if (userList.size() > 0) {
            return ResponseUtil.fail(AUTH_NAME_REGISTERED, "用户名已注册");
        }

        userList = userService.queryByMobile(mobile);
        if (userList.size() > 0) {
            return ResponseUtil.fail(AUTH_MOBILE_REGISTERED, "手机号已注册");
        }
        if (!RegexUtil.isMobileSimple(mobile)) {
            return ResponseUtil.fail(AUTH_INVALID_MOBILE, "手机号格式不正确");
        }
        //判断验证码是否正确
        String cacheCode = CaptchaCodeManager.getCachedCaptcha(mobile);
        if (cacheCode == null || cacheCode.isEmpty() || !cacheCode.equals(code)) {
            return ResponseUtil.fail(AUTH_CAPTCHA_UNMATCH, "验证码错误");
        }

        String openId = "";
        // 非空，则是小程序注册
        // 继续验证openid
        if(!StringUtils.isEmpty(wxCode)) {
            try {
                WxMaJscode2SessionResult result = this.wxService.getUserService().getSessionInfo(wxCode);
                openId = result.getOpenid();
            } catch (Exception e) {
                e.printStackTrace();
                return ResponseUtil.fail(AUTH_OPENID_UNACCESS, "openid 获取失败");
            }
            userList = userService.queryByOpenid(openId);
            if (userList.size() > 1) {
                return ResponseUtil.serious();
            }
            if (userList.size() == 1) {
                LitemallUser checkUser = userList.get(0);
                String checkUsername = checkUser.getUsername();
                String checkPassword = checkUser.getPassword();
                if (!checkUsername.equals(openId) || !checkPassword.equals(openId)) {
                    return ResponseUtil.fail(AUTH_OPENID_BINDED, "openid已绑定账号");
                }
            }
        }

        LitemallUser user = null;
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String encodedPassword = encoder.encode(password);
        user = new LitemallUser();
        user.setUsername(username);
        user.setPassword(encodedPassword);
        user.setMobile(mobile);
        user.setWeixinOpenid(openId);
        user.setAvatar("https://yanxuan.nosdn.127.net/80841d741d7fa3073e0ae27bf487339f.jpg?imageView&quality=90&thumbnail=64x64");
        user.setNickname(username);
        user.setGender((byte) 0);
        user.setUserLevel((byte) 0);
        user.setStatus((byte) 0);
        user.setLastLoginTime(LocalDateTime.now());
        user.setLastLoginIp(IpUtil.getIpAddr(request));
        user.setFSP(0);
        user.setFSPT(0);
        user.setSUN(0);
        user.setSUND(0);
        user.setJianglijin(0);
        user.setXiaofeiquan(0);
        user.setMypay(0);
        user.setKuisun(0);
        user.setServeCenter(0);
        user.setSeller(0);

        // 梨膏糖注册的时候维护用户链字段
        if (userId != null && userId != 0){ // 通过id来找绑定关系
            LitemallUser directInviteeUser = userService.findById(userId);
            if (directInviteeUser != null){
                if (directInviteeUser.getUserlian() != null && !Objects.equals(directInviteeUser.getUserlian(), "0")){
                    user.setUserlian(directInviteeUser.getUserlian()+"," + userId);
                }else {
                    user.setUserlian(userId + "");
                }
                // 直推和间推
                String userlian = user.getUserlian();
                if (userlian != null && !userlian.equals("")){
                    String[] arry = userlian.split(",");
                    user.setDirectInvitee(userId);
                    if (arry.length > 1){ // 间推
                        String indirectInvitee = arry[arry.length-2];
                        if (indirectInvitee != null && !indirectInvitee.equals("")){
                            Integer indirectInvitee1 = Integer.parseInt(indirectInvitee);
                            user.setIndirectInvitee(indirectInvitee1);
                        }
                    }
                }
            }
        }else { // 通过手机号码来绑定关系
            if (recommender != null && !recommender.equals("")){
                LitemallUser userlian =  userService.GetByUsermobile(recommender);
                if(userlian!=null){
                    if (userlian.getUserlian() != null && !Objects.equals(userlian.getUserlian(), "0")){
                        user.setUserlian(userlian.getUserlian()+"," + userlian.getId());
                    }else {
                        user.setUserlian(userlian.getId() + "");
                    }
                    user.setDirectInvitee(userlian.getId());
                    String myuserlian = user.getUserlian();
                    if (myuserlian != null){
                        String[] arry = myuserlian.split(",");
                        if (arry.length > 1){ // 间推
                            logger.info("array:" + Arrays.toString(arry));
                            String indirectInvitee = arry[arry.length-2];
                            logger.info("indirectInvitee:" + indirectInvitee);
                            if (indirectInvitee != null && !indirectInvitee.trim().equals("")){
                                Integer indirectInvitee1 = Integer.parseInt(indirectInvitee);
                                user.setIndirectInvitee(indirectInvitee1);
                            }
                        }
                    }
                }
            }else {
                user.setDirectInvitee(0);
                user.setUserlian(null);
            }
        }

        userService.add(user);



        /*#####################不知道发的什么东西#####################*/
        // 给新用户发送注册优惠券
        couponAssignService.assignForRegister(user.getId());





        //定义一个user对象用来更新用户的直接推荐人和间接推荐人
        /*LitemallUser updateInvitee = new LitemallUser();
        updateInvitee.setId(user.getId());*/


        /*************  完成注册 ，添加 用户链信息        ******************************************************************************************/
        //一、是否存在邀请人
//        LitemallUser yqm=new LitemallUser();
//        String  myuserlian="";
//        if (recommender==null || "".equals(recommender)) {
//            myuserlian ="";
//
//
//        }else
//        {
//            //1.存在邀请人，查出邀请人是谁
//            LitemallUser userlian =  userService.GetByUsermobile(recommender);
//            try{
//                //是否存在真实 链
//                if(userlian!=null ||  null!=userlian.getId()){
//
//
//                    myuserlian = userlian.getUserlian();
//                    //同时还要根据邀请人查出当前注册用户的间接邀请人
//                    String[] arry = myuserlian.split(",");
//                    if (arry.length > 1) {
//                        String indirectInvitee = arry[arry.length-2];
//                        Integer indirectInvitee1 = Integer.parseInt(indirectInvitee);
//                        //给当前注册用户设置间接邀请人
//                        yqm.setIndirectInvitee(indirectInvitee1);
//                        //把邀请人设置为直接邀请人
//                        String directInvitee = arry[arry.length-1];
//                        Integer directInvitee1 = Integer.parseInt(directInvitee);
//                        yqm.setDirectInvitee(directInvitee1);
//
//                    }
//
//
//                }
//            }catch (Exception e){
//
//            }
//
//
//
//        }
//
//
//
//        //这里执行直推人和间推人更新操作
//       // userService.updateById(updateInvitee);
//
//
//        if ("".equals(myuserlian) || myuserlian==null){
//            myuserlian=""+user.getId();
//        }else {
//            myuserlian+= ","+user.getId();
//        }
//
//        yqm.setId(user.getId());
//        //yqm.setUserlian(myuserlian);
//        /*给新注册用户发FSP和FST并且更新直推人和间推人*/
//        yqm.setFSP(100);
//        yqm.setFSPT(100);
//        userService.updateById(yqm);
        /*************************************************************************************************/

        // userInfo
        UserInfo userInfo = new UserInfo();
        userInfo.setNickName(username);
        userInfo.setAvatarUrl(user.getAvatar());

        // token
        String token = UserTokenManager.generateToken(user.getId());

        Map<Object, Object> result = new HashMap<Object, Object>();
        result.put("token", token);
        result.put("userInfo", userInfo);
        result.put("userid", user.getId());// 注册用户的userid
        return ResponseUtil.ok(result);


    }

    /**
     * 请求验证码
     *
     * TODO
     * 这里需要一定机制防止短信验证码被滥用
     *
     * @param body 手机号码 { mobile: xxx, type: xxx }
     * @return
     */
    @PostMapping("captcha")
    public Object captcha(@LoginUser Integer userId, @RequestBody String body) {
        if(userId == null){
            return ResponseUtil.unlogin();
        }
        String phoneNumber = JacksonUtil.parseString(body, "mobile");
        String captchaType = JacksonUtil.parseString(body, "type");
        if (StringUtils.isEmpty(phoneNumber)) {
            return ResponseUtil.badArgument();
        }
        if (!RegexUtil.isMobileSimple(phoneNumber)) {
            return ResponseUtil.badArgumentValue();
        }
        if (StringUtils.isEmpty(captchaType)) {
            return ResponseUtil.badArgument();
        }

        if (!notifyService.isSmsEnable()) {
            return ResponseUtil.fail(AUTH_CAPTCHA_UNSUPPORT, "小程序后台验证码服务不支持");
        }
        String code = CharUtil.getRandomNum(6);
        boolean successful = CaptchaCodeManager.addToCache(phoneNumber, code);
        if (!successful) {
            return ResponseUtil.fail(AUTH_CAPTCHA_FREQUENCY, "验证码未超时1分钟，不能发送");
        }
        notifyService.notifySmsTemplate(phoneNumber, NotifyType.CAPTCHA, new String[]{code});

        return ResponseUtil.ok();
    }

    /**
     * 账号密码重置
     *
     * @param body    请求内容
     *                {
     *                password: xxx,
     *                mobile: xxx
     *                code: xxx
     *                }
     *                其中code是手机验证码，目前还不支持手机短信验证码
     * @param request 请求对象
     * @return 登录结果
     * 成功则 { errno: 0, errmsg: '成功' }
     * 失败则 { errno: XXX, errmsg: XXX }
     */
    @PostMapping("reset")
    public Object reset(@RequestBody String body, HttpServletRequest request) {
        String password = JacksonUtil.parseString(body, "password");
        String mobile = JacksonUtil.parseString(body, "mobile");
        String code = JacksonUtil.parseString(body, "code");

        if (mobile == null || code == null || password == null) {
            return ResponseUtil.badArgument();
        }

        //判断验证码是否正确
        String cacheCode = CaptchaCodeManager.getCachedCaptcha(mobile);
        if (cacheCode == null || cacheCode.isEmpty() || !cacheCode.equals(code))
            return ResponseUtil.fail(AUTH_CAPTCHA_UNMATCH, "验证码错误");

        List<LitemallUser> userList = userService.queryByMobile(mobile);
        LitemallUser user = null;
        if (userList.size() > 1) {
            return ResponseUtil.serious();
        } else if (userList.size() == 0) {
            return ResponseUtil.fail(AUTH_MOBILE_UNREGISTERED, "手机号未注册");
        } else {
            user = userList.get(0);
        }

        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String encodedPassword = encoder.encode(password);
        user.setPassword(encodedPassword);

        if (userService.updateById(user) == 0) {
            return ResponseUtil.updatedDataFailed();
        }

        return ResponseUtil.ok();
    }

    /**
     * 账号手机号码重置
     *
     * @param body    请求内容
     *                {
     *                password: xxx,
     *                mobile: xxx
     *                code: xxx
     *                }
     *                其中code是手机验证码，目前还不支持手机短信验证码
     * @param request 请求对象
     * @return 登录结果
     * 成功则 { errno: 0, errmsg: '成功' }
     * 失败则 { errno: XXX, errmsg: XXX }
     */
    @PostMapping("resetPhone")
    public Object resetPhone(@LoginUser Integer userId, @RequestBody String body, HttpServletRequest request) {
        if(userId == null){
            return ResponseUtil.unlogin();
        }
        String password = JacksonUtil.parseString(body, "password");
        String mobile = JacksonUtil.parseString(body, "mobile");
        String code = JacksonUtil.parseString(body, "code");

        if (mobile == null || code == null || password == null) {
            return ResponseUtil.badArgument();
        }

        //判断验证码是否正确
        String cacheCode = CaptchaCodeManager.getCachedCaptcha(mobile);
        if (cacheCode == null || cacheCode.isEmpty() || !cacheCode.equals(code))
            return ResponseUtil.fail(AUTH_CAPTCHA_UNMATCH, "验证码错误");

        List<LitemallUser> userList = userService.queryByMobile(mobile);
        LitemallUser user = null;
        if (userList.size() > 1) {
            return ResponseUtil.fail(AUTH_MOBILE_REGISTERED, "手机号已注册");
        }
        user = userService.findById(userId);

        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        if (!encoder.matches(password, user.getPassword())) {
            return ResponseUtil.fail(AUTH_INVALID_ACCOUNT, "账号密码不对");
        }

        user.setMobile(mobile);
        if (userService.updateById(user) == 0) {
            return ResponseUtil.updatedDataFailed();
        }

        return ResponseUtil.ok();
    }

    /**
     * 账号信息更新
     *
     * @param body    请求内容
     *                {
     *                password: xxx,
     *                mobile: xxx
     *                code: xxx
     *                }
     *                其中code是手机验证码，目前还不支持手机短信验证码
     * @param request 请求对象
     * @return 登录结果
     * 成功则 { errno: 0, errmsg: '成功' }
     * 失败则 { errno: XXX, errmsg: XXX }
     */
    @PostMapping("profile")
    public Object profile(@LoginUser Integer userId, @RequestBody String body, HttpServletRequest request) {
        if(userId == null){
            return ResponseUtil.unlogin();
        }

        String avatar = JacksonUtil.parseString(body, "avatar");
        Byte gender = JacksonUtil.parseByte(body, "gender");
        String nickname = JacksonUtil.parseString(body, "nickname");

        LitemallUser user = userService.findById(userId);

        if(!StringUtils.isEmpty(avatar)){
            user.setAvatar(avatar);
        }
        if(gender != null){
            user.setGender(gender);
        }
        if(!StringUtils.isEmpty(nickname)){
            user.setNickname(nickname);
        }

        if (userService.updateById(user) == 0) {
            return ResponseUtil.updatedDataFailed();
        }

        return ResponseUtil.ok();
    }

    /**
     * 微信手机号码绑定
     *
     * @param userId
     * @param body
     * @return
     */
    @PostMapping("bindPhone")
    public Object bindPhone(@LoginUser Integer userId, @RequestBody String body) {
    	if (userId == null) {
            return ResponseUtil.unlogin();
        }
    	LitemallUser user = userService.findById(userId);
        String encryptedData = JacksonUtil.parseString(body, "encryptedData");
        String iv = JacksonUtil.parseString(body, "iv");
        WxMaPhoneNumberInfo phoneNumberInfo = this.wxService.getUserService().getPhoneNoInfo(user.getSessionKey(), encryptedData, iv);
        String phone = phoneNumberInfo.getPhoneNumber();
        user.setMobile(phone);
        if (userService.updateById(user) == 0) {
            return ResponseUtil.updatedDataFailed();
        }
        return ResponseUtil.ok();
    }

    @PostMapping("logout")
    public Object logout(@LoginUser Integer userId) {
        if (userId == null) {
            return ResponseUtil.unlogin();
        }
        return ResponseUtil.ok();
    }

    @GetMapping("info")
    public Object info(@LoginUser Integer userId) {
        if (userId == null) {
            return ResponseUtil.unlogin();
        }

        YeJI yeJI = userService.getyejibyuserid(userId);

        LitemallUser user = userService.findById(userId);

        Map<Object, Object> data = new HashMap<Object, Object>();
        data.put("nickName", user.getNickname());
        data.put("avatar", user.getAvatar());
        data.put("gender", user.getGender());
        data.put("mobile", user.getMobile());
        data.put("xiaofeiquan",user.getXiaofeiquan());
        data.put("jianglijin",user.getJianglijin());
        data.put("FSP",user.getFSP());
        data.put("FSPT",user.getFSPT());
        data.put("SUN",user.getSUN());
        data.put("SUND", user.getSUND());
        data.put("userLevel", user.getUserLevel());
        data.put("seller", user.getSeller());
        data.put("serveCenter", user.getServeCenter());
        data.put("teamtMember",yeJI.getTdrr());
        data.put("totalYJ",yeJI.getTdyj());
        data.put("todayYJ",yeJI.getGryj());
        data.put("userid",userId);
        data.put("integral",user.getIntegral());

        return ResponseUtil.ok(data);
    }




    /**
     * 修改支付密码
     *
     * @param body    请求内容
     *                {
     *                paypassword: xxx,
     *            lastpassword:xxx
     *
     *                }
     *
     * @param
     * @return 登录结果
     * 成功则 { errno: 0, errmsg: '成功' }
     * 失败则 { errno: XXX, errmsg:修改失败，请刷新重试 }
     * 失败则 { errno: XXX, errmsg:旧密码错误  }
     *
     */
    @PostMapping("/paypass")
    public Object paypass(@LoginUser Integer userId, @RequestBody String body) {
        if(userId == null){
            return ResponseUtil.unlogin();
        }
        String password = JacksonUtil.parseString(body, "paypassword");
        String lastpassword = JacksonUtil.parseString(body, "lastpassword");


        if (lastpassword == null ||  password == null) {
            return ResponseUtil.badArgument();
        }

    int    nowpass =userService.getpaypass(userId);

int  last=Integer.parseInt(lastpassword);
if(nowpass ==last){
    int   result= userService.setpaypass(userId,Integer.parseInt(password));
if(result>0){
    return ResponseUtil.ok("修改成功");
}else {
    return ResponseUtil.fail(402,"修改失败，请刷新重试");
}

}else {
    return ResponseUtil.fail(402,"旧密码错误");
}


    }

    /**
     *  一个单独 参数 paypassword
     *  成功 返回 1
     *  失败 返回 密码错误  { "paypassword": "xxxxx" }
     * @param userId
     * @return
     */
    @PostMapping("/mypaypass")
    public Object mypay(@LoginUser Integer userId, @RequestBody String body ){
        if(userId == null){
            return ResponseUtil.unlogin();
        }
        String paypassword = JacksonUtil.parseString(body, "paypassword");
        return   userService.getpaypass(userId)==Integer.parseInt(paypassword)?  ResponseUtil.ok(1):ResponseUtil.fail(0,"密码错误");
    }

    /**
     * 直接生成经Base64加密的图片内容，前端可在src内先加入data:image/jpeg;base64,   后面跟上Base64即可完成显示
     * @param userid 用户id
     * @param res
     */
    @PostMapping("getQRCode")
    public Object getQRCode(@LoginUser Integer userid , HttpServletResponse res){
        return ResponseUtil.ok(QrCodeUtils.getminiqrQr(userid, res));
    }

    /**
     * 跟进id获取用户手机号
     * @param userId
     * @return
     */
    @GetMapping("userinfo")
    public Object userinfo(Integer userId) {
        if (userId == null) {
            return ResponseUtil.fail(1,"用户id空");
        }
        LitemallUser user = userService.findById(userId);

        Map<Object, Object> data = new HashMap<Object, Object>();

        data.put("mobile", user.getMobile());

        return ResponseUtil.ok(data);
    }

}
